package ua.exchangeratesukraine.util;

import java.io.IOException;
import java.io.StringReader;
import java.util.ArrayList;

import org.xmlpull.v1.XmlPullParser;
import org.xmlpull.v1.XmlPullParserException;
import org.xmlpull.v1.XmlPullParserFactory;

import ua.exchangeratesukraine.model.CurrencyModel;

import android.util.Log;
import android.util.Xml;

public class XmlParser {

	private final static String	LOG_TAG	= "XmlParser";

	public static ArrayList<CurrencyModel> parsingXml(String xml) {

		if(xml.contains(Constants.BASE_URL))
			return (new ArrayList<CurrencyModel>());
		
		String tmpValue = "";
		String oficialDate = "";
		String titleXml = "";
		String nameTag = "";

		ArrayList<CurrencyModel> unitModelArray = new ArrayList<CurrencyModel>();
		CurrencyModel uModel = null;

		try {
			XmlPullParserFactory factory = XmlPullParserFactory.newInstance();
			// support enable namespace (disable default)
			factory.setNamespaceAware(true);
			// create parser
			XmlPullParser xpp = Xml.newPullParser();
			// set Reader for parser
			xpp.setInput(new StringReader(xml));

			while (xpp.getEventType() != XmlPullParser.END_DOCUMENT) {

				switch (xpp.getEventType()) {
					case XmlPullParser.START_TAG:
						nameTag = xpp.getName();

						if (nameTag.equalsIgnoreCase("Rates")
								|| nameTag.equalsIgnoreCase("CashRates")) {
							oficialDate = xpp.getAttributeValue(0); // Date of exchange list rate
							titleXml = xpp.getAttributeValue(1); // document name
							Log.d(LOG_TAG, titleXml + " на " + oficialDate);
						}

						if ((nameTag.equalsIgnoreCase("Currency") && xpp.getDepth() == 2)
								|| nameTag.equalsIgnoreCase("Bank")) {
							uModel = new CurrencyModel();
							// Log.d(LOG_TAG, "Date = " + date);
						}

						break;
					case XmlPullParser.TEXT:
						tmpValue = xpp.getText();
//						Log.d(LOG_TAG, "tmpValue = " + tmpValue);

						break;

					case XmlPullParser.END_TAG:
						nameTag = xpp.getName();
						if (nameTag.equalsIgnoreCase("Date")) {
//							Log.d(LOG_TAG, "Date depth = " + xpp.getDepth());
							uModel.setDate(tmpValue);
						}
						if (nameTag.equalsIgnoreCase("Code")) {
							uModel.setCode(tmpValue);
						}
						if (nameTag.equalsIgnoreCase("Name")) {
							uModel.setName(tmpValue);
						}
						if (nameTag.equalsIgnoreCase("Nominal")) {
							uModel.setNominal(tmpValue);
						}
						if (nameTag.equalsIgnoreCase("Rate")) {
							uModel.setRate(tmpValue);
						}
						if (nameTag.equalsIgnoreCase("ChangeAbs")) {
							uModel.setChangeAbs(tmpValue);
						}
						if (nameTag.equalsIgnoreCase("ChangeRel")) {
							uModel.setChangeRel(tmpValue);
						}
						if (nameTag.equalsIgnoreCase("Currency") && xpp.getDepth() == 3) {
//							Log.d(LOG_TAG, "Currency depth = " + xpp.getDepth());
							uModel.setCurrency(tmpValue);
						}
						if (nameTag.equalsIgnoreCase("Buy")) {
							uModel.setBuy(tmpValue);
						}
						if (nameTag.equalsIgnoreCase("BuyChangeAbs")) {
							uModel.setBuyChangeAbs(tmpValue);
						}
						if (nameTag.equalsIgnoreCase("BuyChangeRel")) {
							uModel.setBuyChangeRel(tmpValue);
						}
						if (nameTag.equalsIgnoreCase("Sale")) {
							uModel.setSale(tmpValue);
						}
						if (nameTag.equalsIgnoreCase("SaleChangeAbs")) {
							uModel.setSaleChangeAbs(tmpValue);
						}
						if (nameTag.equalsIgnoreCase("SaleChangeRel")) {
							uModel.setSaleChangeRel(tmpValue);
						}
						if ( (nameTag.equalsIgnoreCase("Currency") && xpp.getDepth() == 2)
								|| nameTag.equalsIgnoreCase("Bank")) {
							unitModelArray.add(uModel);
						}
						break;

					default:
						break;

				}

				// next element
				xpp.next();
			}
		} catch (XmlPullParserException e1) {
			e1.printStackTrace();
		} catch (IOException e2) {
			e2.printStackTrace();
		} catch (Exception e3) {
			e3.printStackTrace();
		}
		return unitModelArray;
	}
}